package com.gomokult.servlet.gamelib;

import com.gomokult.entity.Game;
import com.gomokult.exception.NoSuchPageException;
import com.gomokult.service.impl.GamesLibraryServiceImpl;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * 文件：DeleteCommentServlet.java
 * 作者：赵哲群
 * 完成度：已完成
 * 最后修改日期：2023-7-6
 */
@WebServlet("/servlet/QueryGamesServlet")
public class   QueryGamesServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        int page = Integer.parseInt(request.getParameter("page"));
        String email = request.getParameter("email");
        String startDate1 = request.getParameter("startDate")+" 00:00:00";
        String endDate1 = request.getParameter("endDate")+" 23:59:59";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate2 = dateFormat.parse(startDate1);
            Date endDate2 = dateFormat.parse(endDate1);
            Timestamp startDate = new Timestamp(startDate2.getTime());
            Timestamp endDate = new Timestamp(endDate2.getTime());
            List<Game> Games = GamesLibraryServiceImpl.getSingleton().getGamesFromPage(page,email,startDate,endDate);
            Gson gson = new Gson();
            String json = gson.toJson(Games);
            out.print(json);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPageException e) {
            out.print("#NoSuchPageException");
        }
    }
}
